#!/bin/sh
#
# commit -- commit/save a running lab container to image
#
# Copyright (C) 2016-2020 Wu Zhangjin <lzufalcon@163.com>
#

uname | grep -q MINGW && PWD_OPT="-W"
TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd $PWD_OPT)"
. "$TOP_DIR/tools/docker/config" $* >/dev/null

TAG="$(echo $IMAGE: | cut -d ':' -f2)"

if [ -z "$TAG" ]; then
    TAG=local-$(date +"%Y%m%d%H%M%S")
else
    echo $TAG | grep -q "^local-[0-9][0-9]*"
    [ $? -eq 0 ] && TAG=local-$(date +"%Y%m%d-%H%M%S")
fi

IMAGE=$LAB_USER/$CURRENT:$TAG

do_op "docker commit" CONTAINER_NAME "$IMAGE"

do_unlock
set_var IMAGE
do_lock

echo "LOG: new image name saved to $LAB_IMAGE"
